Saturday, March 31, 2007

revelling in my geekdom

I am forever updating the template for this blog: adding something here, removing something there, making big changes every so often, all in the name of making this page fun and easy on the eyes.

A few days ago I decided I wanted to add more games. I've had a sudoku game just above the cartoons at the bottom of the page for ages, and that was fine, but I wanted more. So, I searched around a bit and found Peeptin by Flooble. I put it at the bottom of the sidebar. You've probably played this game before, with a set of jumbled up tiles numbered 1 to 15 in a 4x4 grid, which you need to slide around to put back in order.

And that was fine, as far as that went. But, I wanted another game, something a little more challenging than Peeptin. So, I went looking for a javascript chess program that I could put in the sidebar. And, I found a fairly decent one, here.

This is where my geekiness comes in. I took the original program and shrunk down the images of the board and pieces so that the game would fit in the sidebar. Then I started playing it. Well, it isn't the strongest player, certainly not the strongest chess program that I've played against, but it was fairly adequate for a little diversion. But, I started noticing some annoying little bugs in the program.

When I was about 12 years old, my great-grandfather gave me some advice: "Learn lots of languages". I'm sure that he thought that he was advising me to learn lots of human languages, and I did study French in high school and German in university. But where I really followed his advice was in learning computer languages.

Over the last couple of dozen years I learned language after language (probably about 15 or 20 so far, I've lost count), and it became easier and easier to pick up more as I went along. After a while, computer languages all look very similar, and it merely becomes a matter of figuring out the quirks of syntax, the similarities and differences to other languages.

I've never programmed in javascript before, but I wanted to get rid of those bugs. So I started looking at the code, and my nightmare began. None of the code was documented, nor was it indented. The variable names and function names were non-descriptive names like "au" or "Z" or "ma". The programmers reading this out there probably shuddered when they read those last two sentences.

So, I went through the code and put in indents where I thought they should go, and started making a list of function names and variable names, with descriptions of what I thought each one did. Along the way, I was learning javascript by reading the code.

The bugs in the program were:
- allowed castling while in check
- castling on the queen side was intermittently disallowed even though the conditions for allowing castling were met
- it wouldn't let either king into square A8
- pawns would only promote to queens, no matter what was selected in the drop-down list
- if "CPU White" was selected, the human player still had to make the first move for the computer
- the border around the board looked strange when the board was shrunk down to a size that would fit in the sidebar

Then there were some things that weren't bugs per se, but still annoying. The original programmer hadn't included the rule that if a board arrangement repeated three times, then it is a stalemate. The player could only play one game; to start over or to play another game, one had to reload the entire webpage (which isn't really a good thing when one's page is as big as this one). Finally, the game log would only display the first 60 moves of the game.

So, I started playing with the code. I added the New Game button, and changed the game log so that it would display an unlimited number of moves in a scrolling panel. Then one by one I tackled the bugs, and with a lot of experimentation I managed to get them all worked out. I modified the pawn promotion algorithm a little so that the human player could choose what piece to be promoted to, but still forced the computer to promote its pawns to queens. Finally I added the code necessary to compare the current board arrangement with previous arrangements, counting the number of duplicate occurrences and declaring a stalemate if the board arrangement repeated three times.

I'm not done with this chess program yet. I have a number of ideas of ways to make it a better player. By the time I am done, there will be some radio buttons to select a skill level for the computer. After that, I will post the revised code, so that anyone who wants to have this corrected and improved javascript chess program on their website can just copy and paste the code.

Over the course of doing this I learned a lot about javascript programming, and I still want more sidebar games. I think one of these days I'm going to write a version of Tetris for the sidebar, and I might just rewrite the whole sudoku section as well.

No comments: